/*
** Mortem's HGE Addon 1.4
** Copyright (C) 2006-2007, Osokin 'elmortem' Makar
** http://elmortem.mirgames.ru
**
** Alternative config. Read only.
*/

#include <mhacfg.h>

#include <windows.h>
#include <stdio.h>

HGE_CHAR mhaCfgFileName[260];
HGE_CHAR mhaCfgString[1024];

void mhaCfg_Init(const HGE_CHAR *filename) {
	HGE_SCPY(mhaCfgFileName, filename);
}

int mhaCfg_GetInt(const HGE_CHAR *section, const HGE_CHAR *name, int def_val) {
	HGE_CHAR buf[260];

	if(mhaCfgFileName[0]) {
		if(GetPrivateProfileString(section, name, TXT(""), buf, sizeof(buf), mhaCfgFileName))
			return HGE_STOI(buf);
		else
			return def_val;
	}
	return def_val;
}

float mhaCfg_GetFloat(const HGE_CHAR *section, const HGE_CHAR *name, float def_val) {
	HGE_CHAR buf[260];

	if(mhaCfgFileName[0]) {
		if(GetPrivateProfileString(section, name, TXT(""), buf, sizeof(buf), mhaCfgFileName))
			return (float)HGE_STOF(buf);
		else
			return def_val;
	}
	return def_val;
}

HGE_CHAR* mhaCfg_GetString(const HGE_CHAR *section, const HGE_CHAR *name, const HGE_CHAR *def_val) {
	if(mhaCfgFileName[0]) GetPrivateProfileString(section, name, def_val, mhaCfgString, sizeof(mhaCfgString), mhaCfgFileName);
	else HGE_SCPY(mhaCfgString, def_val);
	return mhaCfgString;
}

mhaIni::mhaIni(const HGE_CHAR *filename) {
	HGE_SCPY(FileName, filename);
}

mhaIni::~mhaIni(void) {
}

int mhaIni::GetInt(const HGE_CHAR *section, const HGE_CHAR *name, int def_val) {
	HGE_CHAR buf[260];

	if(FileName[0]) {
		if(GetPrivateProfileString(section, name, TXT(""), buf, sizeof(buf), FileName))
			return HGE_STOI(buf);
		else
			return def_val;
	}
	return def_val;
}

float mhaIni::GetFloat(const HGE_CHAR *section, const HGE_CHAR *name, float def_val) {
	HGE_CHAR buf[260];

	if(FileName[0]) {
		if(GetPrivateProfileString(section, name, TXT(""), buf, sizeof(buf), FileName))
			return (float)HGE_STOF(buf);
		else
			return def_val;
	}
	return def_val;
}

HGE_CHAR* mhaIni::GetString(const HGE_CHAR *section, const HGE_CHAR *name, const HGE_CHAR *def_val) {
	if(FileName[0])
		GetPrivateProfileString(section, name, def_val, String, sizeof(String), FileName);
	else
		HGE_SCPY(String, def_val);
	for(int i = 0; i < HGE_SLEN(String); i++) {
		if(String[i] == TXT('|')) String[i] = TXT('\n');
	}
	return String;
}

void mhaIni::SetInt(const HGE_CHAR *section, const HGE_CHAR *name, int val) {
	HGE_CHAR buf[256];
	if(FileName[0]) {
		HGE_SPRINTF(buf,TXT("%d"),val);
		WritePrivateProfileString(section, name, buf, FileName);
	}
}

void mhaIni::SetFloat(const HGE_CHAR *section, const HGE_CHAR *name, float val) {
	HGE_CHAR buf[256];
	if(FileName[0]) {
		HGE_SPRINTF(buf,TXT("%f"),val);
		WritePrivateProfileString(section, name, buf, FileName);
	}
}

void mhaIni::SetString(const HGE_CHAR *section, const HGE_CHAR *name, const HGE_CHAR *val) {
	HGE_CHAR buf[1024];
	HGE_SCPY(buf, val);
	for(int i = 0; i < HGE_SLEN(val); i++) {
		if(buf[i] == TXT('\n')) {
			buf[i] = TXT('|');
		}
	}
	if(FileName[0]) {
		WritePrivateProfileString(section, name, buf, FileName);
	}
}
